﻿using System;
using System.Collections;
using System.Linq;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class TodaySammaryViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// 
        /// </summary>
        IFinanceService _FinanceService;

        /// <summary>
        /// Initializes a new instance of the <see cref="TodaySammaryViewModel"/> class.
        /// </summary>
        /// <param name="administrationRepository">The administration repository.</param>
        /// <param name="FinanceService">The finance service.</param>
        public TodaySammaryViewModel(IAdministrationRepository administrationRepository, IFinanceService FinanceService)
        {
            _AdministrationRepository = administrationRepository;
            _FinanceService = FinanceService;
        }

        /// <summary>
        /// Gets the process balances for committee.
        /// </summary>
        /// <param name="committeeID">The committee ID.</param>
        /// <returns></returns>
        public IEnumerable GetProcessBalancesForCommittee(Guid committeeID)
        {
            //  var committee = _AdministrationRepository.GetCommitteeByID(committeeID);
            var projects = _AdministrationRepository.GetUserProjects(RegistryInfo.UserID);


            return (from project in projects
                    let LockerBalance = _FinanceService.GetPaidReciepts(project.ProjectID).Sum(rec => rec.ReceiptAmount)

                    let projectBaAcs = project.CommitteeBankAccounts.Where(comBA => comBA.CommitteeID.Equals(committeeID))

                    let maDeposites = projectBaAcs.SelectMany(bankAcc => bankAcc.CommitteeBankAccountMainBranchDeposites).Sum(
                    depo => depo.DepositAmount)

                    let deposites = projectBaAcs.SelectMany(bankAcc => bankAcc.CommitteeBankAccountDeposites).SelectMany(
                    depo => depo.CommitteeBankAccountDepositReceipts).Sum(rec => rec.Receipt.ReceiptAmount)

                    let expenses = projectBaAcs.SelectMany(baAcc => baAcc.CommitteeBankAccountExpenses).Sum(
                    exp => exp.WithdrawAmount)

                    select new
                    {
                        ProjectName = project.ProjectName,
                        LockerBalance,
                        BankBalance = maDeposites + deposites - expenses
                    });

        }
    }
}
